using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Gateway : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 1; i <= 31; i++)
            {
                day.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for(int i = 1; i <= 12; i++)
            {
                month.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = DateTime.Today.Year; i >= (DateTime.Today.Year - 100); i--)
            {
                year.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            //Check age allowed
            int ageAllowed = 0;
            System.Xml.XmlNode node = dsCounrties.GetXmlDocument().SelectSingleNode(string.Format("/countries/country[@name='{0}']", country.SelectedValue));
            try
            {
                ageAllowed = int.Parse(node.Attributes["age"].Value);
            }
            catch
            {
            }
            if (ageAllowed == 0)
            {
                pnlTooYoung.Visible = true;
                pnlInput.Visible = false;
            }
            else
            {
                DateTime dob = new DateTime(int.Parse(year.SelectedValue), int.Parse(month.SelectedValue), int.Parse(day.SelectedValue));
                if (AgeAt(dob, DateTime.Now) < ageAllowed)
                {
                    pnlTooYoung.Visible = true;
                    pnlInput.Visible = false;
                }
                else
                {
                    //Set session
                    Session["Validated"] = true;
                    //Set cookie
                    if (cbRember.Checked)
                    {
                        HttpCookie c = new HttpCookie("Validated", true.ToString());
                        c.Expires = DateTime.Now.AddYears(1);
                        Page.Response.Cookies.Add(c);
                    }
                    Response.Redirect("~/default.aspx");
                }
            }
        }
    }

    private int AgeAt(DateTime dateOfBirth, DateTime compareDate)
    {
        int val = 0;
        if (dateOfBirth != DateTime.MinValue && compareDate != DateTime.MinValue)
        {
            val = compareDate.Year - dateOfBirth.Year;
            if (compareDate.Month < dateOfBirth.Month || (compareDate.Month == dateOfBirth.Month && compareDate.Day < dateOfBirth.Day))
            {
                val--;
            }
        }
        return val;
    }

    protected void valDOB_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        try
        {
            DateTime dob = new DateTime(int.Parse(year.SelectedValue), int.Parse(month.SelectedValue), int.Parse(day.SelectedValue));
            e.IsValid = dob < DateTime.Today;
        }
        catch
        {
            e.IsValid = false;
        }

    }
}
